#!/bin/bash

EXTRACTION_DIR="../../Liferay/Extracted"
S3CMD_BIN="/Users/sdd/GLP/Bigstore/s3sync.rb/s3cmd.rb"  # --debug --verbose
BUCKET="glp-rec-library"
BASE_URL="http://commondatastorage.googleapis.com"
export AWS_S3_HOST="commondatastorage.googleapis.com"

echo "Removing existing bucket..."
${S3CMD_BIN} deletebucket ${BUCKET}
echo "Creating bucket..."
${S3CMD_BIN} createbucket ${BUCKET}
for header_path in ${EXTRACTION_DIR}/*.headers.txt; do
    source_name=$(basename ${header_path})
    bucket=$(echo ${source_name} | sed 's/:.*//')
    file_base=$(echo ${source_name} | sed 's/.*://;s/.headers.txt$//')
    data_path=$(ls ${EXTRACTION_DIR}/${bucket}:${file_base}* | grep -v headers.txt)
    dst_name=$(basename ${data_path} | sed 's/^.*://')
    if [[ $(grep -c '"' ${header_path}) != "0" ]]; then
	echo "Illegal characaters in ${header_path}"
	exit 3
    fi
    headers=`cat ${header_path} | sed 's/^META-/x-amz-meta-/;s/^/"/;s/$/"/;s/: /:/'`' "x-amz-acl:public-read"'
    echo ${data_path} '=>' ${BUCKET}:${dst_name}
    cmd="${S3CMD_BIN} put ${BUCKET}:${dst_name} ${data_path} ${headers}"
    eval ${cmd}
done
